﻿using System;
using Krile.Kernel.Bridges;
using System.Text.RegularExpressions;

namespace Krile.Subsystem
{
    public static class CommonOperation
    {
        public static void CallBrowser(string uri)
        {
            try
            {
                if (String.IsNullOrEmpty(Core.Config.Network.BrowserPath))
                    System.Diagnostics.Process.Start(uri);
                else
                    System.Diagnostics.Process.Start(Core.Config.Network.BrowserPath, uri);
            }
            catch (System.ComponentModel.Win32Exception) { }
            catch (Exception e)
            {
                Subsystem.Debugger.AddReport(e);
            }
        }

        internal static bool IsMatchEx(this Regex rgx, string arg)
        {
            return !String.IsNullOrEmpty(arg) && rgx.IsMatch(arg);
        }
    }

    public class __CommmonOperationBridge : ICommonOperation
    {
        public void CallBrowser(string uri)
        {
            Subsystem.CommonOperation.CallBrowser(uri);
        }

        public string AESDecrypt(string cryptograph)
        {
            return Subsystem.Security.DecryptString(cryptograph);
        }

        public string AESEncrypt(string source)
        {
            return Subsystem.Security.EncryptString(source);
        }

        public Type XMLLoad<Type>(string path)
        {
            return Krile.Imported.Snippets.Files.LoadXML<Type>(path, true);
        }

        public void XMLSave<Type>(Type obj, string path)
        {
            Krile.Imported.Snippets.Files.SaveXML<Type>(path, obj);
        }

        public Krile.Kernel.Data.SearchDataCluster ConfigSearchData(Krile.Kernel.Data.SearchDataCluster cluster)
        {
            using (var c = new Forms.Dialogs.Editor.SearchData.ClusterEditor(cluster))
            {
                if (c.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    return c.SearchDataCluster;
                else
                    return null;
            }
        }
    }
}
